#include "gd32e23x.h"
#include "TM1638.h"
#include "pos_param.h"

//8?led???????led1-led8
const uint8_t TM1638_AddrTab[] ={0xC1,0xC3,0xC5,0xC7,0xC9,0xCB,0xCD,0xCF};


/*******************************************************************************
  * ????TM1638_WriteData
  * ?  ??TM1638???
  * ?  ??u8Data???????
  * ?????
  * ?  ???
*******************************************************************************/
void TM1638_WriteData(uint8_t Data)
{
	uint8_t i;
	//TM1638_DIOModeOutput();//DIO?????
	for(i = 0; i < 8; i++)
	{
		TM1638_CLK_OFF;
		(Data & 0x01) ? (TM1638_DIO_ON):(TM1638_DIO_OFF);
		Data >>= 1;
		TM1638_CLK_ON;
	}
}
/*******************************************************************************
  * ????TM1638_ReadData
  * ?  ??TM1638???
  * ?  ???
  * ?????????
  * ?  ???
*******************************************************************************/
uint8_t TM1638_ReadData(void)
{
	uint8_t i;
	uint8_t temp=0;
	//TM1638_DIOModeInput();//DIO?????
	for(i = 0; i < 8; i++)
	{
		temp >>= 1;
		TM1638_CLK_OFF;
		if(TM1638_DIO_RD == 1)
		{
			temp |= 0x80;			
		}
		TM1638_CLK_ON;
	}
	return temp;
}
/*******************************************************************************
  * ????TM1638_ReadKey
  * ?  ??TM1638?????
  * ?  ???
  * ?????????
  * ?  ??????????8???????1~8???Key1~Key8;
			??????K3????????????????????
*******************************************************************************/
uint8_t TM1638_ReadKey(void)
{
	uint8_t u8Data[4], i;
	uint8_t u8Key = 0;
	TM1638_STB_OFF;
	TM1638_WriteData(0x42);
	for (i = 0; i < 4; i++)
	{
		u8Data[i] = TM1638_ReadData();//?BYTE1~BYTE4???
	}
	TM1638_STB_ON;
	for (i = 0; i < 4; i++)//??4?BYTE????
	{
		if (((u8Data[i] >> 0) & 0x01) == 0x01)//?0??B0,?????1?3?5?7??????i*2+1
		{
			u8Key = (i * 2 + 1);
			break;
		}else
		{
			if (((u8Data[i] >> 4) & 0x01) == 0x01)//?4??B4,?????2?4?6?8??????i*2+2
			{
				u8Key = (i * 2 + 2);
				break;
			}
		}
	}
	return u8Key;
}
/*******************************************************************************
  * ????TM1638_WriteCmd
  * ?  ??TM1638????
  * ?  ??u8Cmd???????
  * ?????
  * ?  ????????STB?0,????1
*******************************************************************************/
void TM1638_WriteCmd(uint8_t Cmd)
{
	TM1638_STB_OFF;
	TM1638_WriteData(Cmd);
	TM1638_STB_ON;
}
/**
  * TM1638?????????
  * ??1?addr?????????
  * ??2?data?????8???
  * ?????
  */
void TM1638_WriteOneData(unsigned char addr,unsigned char data) //TM1638?????????
{
	//TM1638_WriteCmd(0x44);
	TM1638_STB_OFF; //STB=0
	TM1638_WriteData(0xc0|addr); //??
	TM1638_WriteData(data); //??
	TM1638_STB_ON; //STB=1
}
/*******************************************************************************
  * ????TM1638_SetBrightness
  * ?  ??????
  * ?  ??u8Brt????8??0~7
  * ?????
  * ?  ??0x88????
*******************************************************************************/
void TM1638_SetBrightness(uint8_t u8Brt)
{
	TM1638_WriteCmd(0x88 | u8Brt);
}
/*******************************************************************************
  * ????TM1638_Switch
  * ?  ??????
  * ?  ??0??1?
  * ?????
  * ?  ??0x88????
*******************************************************************************/
void  TM1638_Switch(uint8_t bState)
{
	bState ? TM1638_WriteCmd(0x88) : TM1638_WriteCmd(0x80);
}

void Init_TM1638(void)
{
	unsigned char i;
    rcu_periph_clock_enable(RCU_GPIOB);

#ifdef ZSY03_V2206
    gpio_mode_set ( GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_5|GPIO_PIN_4 );
    gpio_output_options_set ( GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5|GPIO_PIN_4 );
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
#else
    gpio_mode_set ( GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_7|GPIO_PIN_6 );
    gpio_output_options_set ( GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_6 );
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_5);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
#endif

//    gpio_mode_set ( GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_0 );
//    gpio_output_options_set ( GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 );
	
	TM1638_WriteCmd(0x88 | PosParam.LedLumin);      //?? (0x88-0x8f)8?????
	TM1638_WriteCmd(0x40);       ////???????1; 0x44???????????????????

	TM1638_STB_OFF;		         
	TM1638_WriteData(0xc0);    //??????
	for(i=0;i<16;i++){	   //??16??????
		TM1638_WriteData(0x00);
	}
	TM1638_STB_ON;
}

